package cn.vcjf.washcar.server.controller;


import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.dict.entity.Dict;
import cn.vcjf.washcar.dict.service.IDictService;
import cn.vcjf.washcar.server.entity.ServerInfo;
import cn.vcjf.washcar.server.service.ServerInfoService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务种类信息表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-17
 */
@Controller
@RequestMapping("/server/serverInfo")
public class ServerInfoController {
	@Resource
	private ServerInfoService serverInfoService;
	@Resource
	private IDictService dictService;


	@RequestMapping("/addServerInfo")
	@ResponseBody
	public TableResult addServerInfo(ServerInfo serverInfo) {
		serverInfo.preInsert();
		boolean b = serverInfoService.insert(serverInfo);
		if (b) {
			Dict dict = new Dict();
			dict.setLabel(serverInfo.getServerName() + "(" + serverInfo.getRemakes() + ")");
			dict.setValue(serverInfo.getId() + "");
			dict.setDescription("服务类型");
			dict.setRemarks("服务类型:" + serverInfo.getServerName());
			dict.setType("washCarOrder_serverType");
			boolean insert = dictService.insert(dict);
			return TableResult.Result(insert);
		}
		return TableResult.Result(b);
	}

	@RequestMapping("/updateServerInfo")
	@ResponseBody
	public TableResult updateServerInfo(ServerInfo serverInfo) {
		serverInfo.preUpdate();
		boolean b = serverInfoService.updateById(serverInfo);
		return TableResult.Result(b);
	}

	@RequestMapping("/findServerInfoOne")
	@ResponseBody
	public TableResult findServerInfoOne(ServerInfo serverInfo) {

		EntityWrapper<ServerInfo> wrapper = serverInfo.getQueryWrapper();

		ServerInfo result = serverInfoService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findServerInfoList")
	@ResponseBody
	public TableResult findServerInfoList(ServerInfo serverInfo) {

		EntityWrapper<ServerInfo> wrapper = serverInfo.getQueryWrapper();

		Page<ServerInfo> page = serverInfo.getPageInfo();
		wrapper.orderBy("createTime", false);
		page = serverInfoService.selectPage(page, wrapper);

		return TableResult.Result(page);

	}

	@RequestMapping("/deleteServerInfoByIds")
	@ResponseBody
	public TableResult deleteServerInfoByIds(String... ids) {
		List<String> list = Arrays.asList(ids);
		boolean b = serverInfoService.deleteBatchIds(list);
		return TableResult.Result(b);
	}
}
